package eerbaar.client.internal.ctr.operator;

import eerbaar.client.ctr.OperatorConstants;
import eerbaar.client.exp.ExpException;
import eerbaar.client.exp.IExp;
import eerbaar.client.exp.IScope;
import eerbaar.client.exp.IState;
import eerbaar.client.exp.ScopeManager;
import eerbaar.client.internal.ctr.operator.area.ValueArea;
import eerbaar.client.internal.ctr.operator.area.ValueAreaPopulator;
import eerbaar.game.IGame;
import eerbaar.persona.IPersona;
import eerbaar.position.IPosition;
import eerbaar.position.PositionFactory;

class GotoRandom implements IExp {

    public GotoRandom() {
        //
    }

    @Override
    public IPosition calcDestination(IScope scope) {

        IExp[] exps = new IExp[] { this };

        ValueArea va = (ValueArea) scope.getValueArea();
        IGame game = (IGame) scope.getGame();
        if (va == null) {
            va = new ValueArea(game);
            scope.setValueArea(va);
        }
        ValueAreaPopulator populator = new ValueAreaPopulator(va, game, exps);

        IPosition p = null;
        {
            ScopeManager manager = scope.getScopeManager();
            IScope subScope = manager.pushScope("sub");
            subScope.setValueArea(populator.getValueArea());

            p = populator.calcDestination(subScope);

            manager.popScope();
        }

        return p;
    }

    @Override
    public IScope interpret(IScope inScope) throws ExpException {

        IScope scope = inScope;

        IPersona pet = (IPersona) scope.getTarget();
        IGame game = (IGame) scope.getGame();

        IExp exp = null;
        IPosition p = calcDestination(scope);
        if (p != null) {
            exp = Global.calcMoveTo(game, pet, pet.getPosition(), p);
        }

        IState state = scope.getState();
        if (exp == null) {
            state.setFinished(true);
        } else {
            exp.interpret(scope);
            state.setFinished(false);
        }

        return scope;
    }

    @Override
    public boolean isInteresting(String elementName) {
        return false;
    }

    @Override
    public void populateValueArea(IScope scope) {
        //
    }

    private IPosition randomPos(IPersona pet) {

        int x = pet.getPosition().getX();
        int y = pet.getPosition().getY();

        boolean found = false;

        for (int i = 0; i < 3; i++) {

            int n = new java.util.Random().nextInt(4);
            // int n = Random.nextInt(4);
            if (n == 0) {
                x++;
                found = true;
            } else if (n == 1) {
                x--;
                found = true;
            } else if (n == 2) {
                y--;
                found = true;
            } else {
                y++;
                found = true;
            }

            if (x < 0 || x > 9) {
                x = pet.getPosition().getX();
                found = false;
            }
            if (y < 0 || y > 9) {
                y = pet.getPosition().getY();
                found = false;
            }

            if (found) {
                break;
            }
        }

        return PositionFactory.createPosition(x, y);
    }

    @Override
    public String toString() {

        return OperatorConstants.RANDOM;
    }

    @Override
    public void updateArea(IScope scope) {

        IGame game = (IGame) scope.getGame();
        ValueArea area = (ValueArea) scope.getValueArea();

        IPersona pet = game.getPersona("pet");
        IPosition p = randomPos(pet);
        area.incrementArea(p.getX(), p.getY(), 2, 1, 1);
    }

}
